<?php

namespace app\model\tmp;

use think\facade\Log;
use think\Model;

class CustomerInfoTmp extends Model
{

    protected $table = 'customer_info_tmp';

    public function modifyDataById(int $customerId, array $data)
    {
        return $this->where(['id' => $customerId])->update($data);
    }

    public function customerList(array $params)
    {
        $fields = [
            '`id` as customer_id',
            '`username`',
            '`organization_name`',
            '`organization_quality`',
            '`mobile`',
            'user_area',
            '`communicate_records`',
            '`next_time`',
            '`visit_status`',
            '`customer_source`',
            '`customer_type`',
            '`accumulation_fund`'
        ];
        $execute = $this;
        if (!empty($params['filter_type'])) {
            if ($params['filter_type'] == 2) {
                $execute = $execute->where('customer_type', '=', 1);
            } elseif ($params['filter_type'] == 3) {
                $execute = $execute->where('customer_type', '=', 2);
            } elseif ($params['filter_type'] == 4) {
                $execute = $execute->where('customer_source', '=', '短信');
            } elseif ($params['filter_type'] == 5) {
                $execute = $execute->where('customer_type', '=', 5);
            } elseif ($params['filter_type'] == 6) {
                $execute = $execute->where('customer_type', '=', 6);
            }
        }
        if (!empty($params['keywords'])) {
            $execute = $execute->where('mobile', 'like', '%' . $params['keywords'] . '%');
        }
        return $execute
            ->field($fields)
            ->order(['next_time' => 'asc', 'id' => 'desc'])
            ->paginate(5, false)
            ->toArray();
    }

    public function customerInfo(int $customerId)
    {
        $where = [
            'id' => $customerId,
        ];
        $field = [
            '`id` as customer_id',
            '`username`',
            '`organization_name`',
            'user_area',
            '`organization_quality`',
            '`mobile`',
            '`communicate_records`',
            '`next_time`',
            '`visit_status`',
            '`customer_source`',
            '`customer_type`',
            '`accumulation_fund`'
        ];
        return $this
            ->where($where)
            ->field($field)
            ->findOrEmpty()
            ->toArray();
    }

    public function birthdayNotice()
    {
        $where = [
            'birthday_day' => date('md'),
        ];
        $field = [
            '`id` as customer_id',
            '`username`',
            '`birthday`',
        ];
        return $this
            ->where($where)
            ->field($field)
            ->select()
            ->toArray();
    }

    public function idKeyList()
    {
        $field = [
            '`id` as customer_id',
            '`username`',
            '`birthday`',
        ];
        return $this->column($field, 'id');
    }
}